﻿@using DevExpress.Blazor
@using DevExpress.Blazor.Internal
@using EasyXaf.LowCode.WorkflowEditors.Models.Activities
@using Microsoft.AspNetCore.Components.Web

<div class="sequence-node @(CssClass)">
    @foreach (var activity in Sequence.Activities)
    {
        if (activity is StartActivity startActivity)
        {
            if (Sequence is IBranch branch)
            {
                <BranchStartNode @key="startActivity"
                                 StartActivity="startActivity"
                                 AddActivityClicked="(e)=>AddActivityAsync(startActivity,e)" />
            }
            else
            {
                <StartNode @key="startActivity"
                           StartActivity="startActivity"
                           AddActivityClicked="(e)=>AddActivityAsync(startActivity,e)" />
            }
        }
        else if (activity is EndActivity endActivity)
        {
            if (Sequence is IBranch branch)
            {
                <BranchEndNode @key="endActivity" EndActivity="endActivity" Branch="branch" />
            }
            else
            {
                <EndNode @key="endActivity" EndActivity="endActivity" />
            }
        }
        else if (activity is IBranchesActivity branchesActivity)
        {
            <BranchesNode @key="branchesActivity"
                          BranchesActivity="branchesActivity"
                          AddActivityClicked="(e)=>AddActivityAsync(branchesActivity,e)"
                          AddBranchClicked="()=>AddBranch(branchesActivity)" />
        }
        else
        {
            <ActivityNode @key="activity"
                          Activity="activity"
                          AddActivityClicked="(e)=>AddActivityAsync(activity,e)" />
        }
    }
</div>

<DxContextMenu @ref="ActivityToolboxContextMenu">
    <Items>
        <DxContextMenuItem>
            <Template>
                <div class="workflow-activity-toolbox d-flex" @onclick:stopPropagation>
                    <DxScrollViewer>
                        <DxAccordion FilterMinLength="1"
                                     ShowFilterPanel="true"
                                     SizeMode="SizeMode.Small">
                            <Items>
                                @foreach (var toolItemGroup in GetActivityToolItemGroups())
                                {
                                    <DxAccordionItem @key="toolItemGroup"
                                                     Name="@(toolItemGroup.Id)"
                                                     Text="@(toolItemGroup.Caption)"
                                                     Expanded="toolItemGroup.IsExpand">
                                        <Items>
                                            @foreach (var toolItem in toolItemGroup.Children)
                                            {
                                                <DxAccordionItem @key="toolItem"
                                                                 Name="@(toolItem.Id)"
                                                                 Text="@(toolItem.Caption)"
                                                                 IconUrl="@(toolItem.IconUrl)"
                                                                 Click="async()=>await ActivityToolItemClickAsync(toolItem)" />
                                            }
                                        </Items>
                                    </DxAccordionItem>
                                }
                            </Items>
                        </DxAccordion>
                    </DxScrollViewer>
                </div>
            </Template>
        </DxContextMenuItem>
    </Items>
</DxContextMenu>

<style>
    .workflow-activity-toolbox {
        min-width: 250px;
        max-height: 400px;
    }

        .workflow-activity-toolbox .dxbl-accordion-item-content .dxbl-image.image {
            width: 16px;
            height: 16px;
        }

        .workflow-activity-toolbox .dxbl-accordion-item-content .dxbl-accordion-group-expand-btn-container {
            width: 16px;
        }
</style>